package com.shundr.auth.user.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.table.TableUtils;
import com.shundr.auth.common.model.AreaInfo;
import com.shundr.auth.common.util.DateUtils;
import com.shundr.auth.frame.db.LDaoManager;
import com.shundr.auth.frame.util.LLogger;
import com.shundr.auth.user.model.UserInfo;
import com.shundr.truck.model.TruckInfo;

/**
 * userDao UserDao
 * 
 * @author larry 2015-4-10 下午3:02:24
 * @version 1.0.0
 *
 */
public class TruckDao {

	private Dao<TruckInfo, Integer> truckDao;

	public TruckDao() {
		truckDao = LDaoManager.createDao(TruckInfo.class);
	}

	/**
	 * 
	 * insert(这里用一句话描述这个方法的作用) 插入用户 (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param userInfo
	 *            void
	 * @exception
	 * @since 1.0.0
	 */
	public int insert(TruckInfo truckInfo) {
		try {
			return truckDao.create(truckInfo);
		} catch (SQLException e) {
			LLogger.e("创建数据类型时异常" + e);
		}
		return 0;
	}

	/**
	 * 更新
	 * 
	 * @param truckInfo
	 */
	public void update(TruckInfo truckInfo) {
		try {
			truckDao.update(truckInfo);
		} catch (SQLException e) {
			LLogger.e("数据更新时异常" + e);
		}
	}

	public void updateMore(final List<TruckInfo> list) {
		if (list == null || list.size() == 0)
			return;
		try {
			truckDao.callBatchTasks(new Callable<Void>() {

				@Override
				public Void call() throws Exception {
					// userDao.deleteBuilder().delete();
					for (TruckInfo info : list) {
						truckDao.update(info);
					}
					return null;
				}
			});
		} catch (Exception e) {
			LLogger.e("更新数据类型时发生异常 ： " + e);
		}
	}

	/**
	 * 查詢是否已經存在
	 * 
	 * @param phone
	 * @return
	 */
	public boolean checkExistByPhone(String phone) {
		try {
			QueryBuilder<TruckInfo, Integer> queryBuilder = truckDao
					.queryBuilder();
			queryBuilder.where().eq("truckerPhone", phone);
			return queryBuilder.query().size() > 0;
		} catch (SQLException e) {
			LLogger.e("查询时发生异常 ： " + e);
		}
		return false;
	}

	/**
	 * 獲取摸個時間段的車輛信息
	 * 
	 * @param begin
	 * @param end
	 * @return
	 */
	public List<TruckInfo> getAllTruckInfosByDate(Date begin, Date end) {
		try {
			String beginStr = DateUtils.getDayBeginString(begin);
			String endStr = DateUtils.getDayEndString(end);
			QueryBuilder<TruckInfo, Integer> queryBuilder = truckDao
					.queryBuilder();
			queryBuilder.where().lt("truckUpdateTime", endStr).and()
					.gt("truckUpdateTime", beginStr);
			return queryBuilder.query();
		} catch (SQLException e) {
			LLogger.e("查询时发生异常 ： " + e);
		}
		return new ArrayList<TruckInfo>();
	}

	/**
	 * 獲取摸個時間段的某種狀態的車輛信息
	 * 
	 * @param upStatus
	 *            0-未上传，1-上传成功，2-上传失败
	 * @param begin
	 * @param end
	 * @return
	 */
	public List<TruckInfo> getTruckListByStatusAndDate(int upStatus,
			Date begin, Date end) {
		try {
			String beginStr = DateUtils.getDayBeginString(begin);
			String endStr = DateUtils.getDayEndString(end);
			QueryBuilder<TruckInfo, Integer> queryBuilder = truckDao
					.queryBuilder();
			queryBuilder.where().lt("truckUpdateTime", endStr).and()
					.gt("truckUpdateTime", beginStr).and()
					.eq("upStatus", upStatus);
			return queryBuilder.query();
		} catch (SQLException e) {
			LLogger.e("查询时发生异常 ： " + e);
		}
		return new ArrayList<TruckInfo>();
	}

	public List<TruckInfo> getAllTruckInfosByStatus(int status) {
		try {
			QueryBuilder<TruckInfo, Integer> queryBuilder = truckDao
					.queryBuilder();
			queryBuilder.where().eq("upStatus", status);
			return queryBuilder.query();
		} catch (SQLException e) {
			LLogger.e("查询时发生异常 ： " + e);
		}
		return new ArrayList<TruckInfo>();
	}

	/**
	 * 删除用户 delete(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param info
	 *            void
	 * @exception
	 * @since 1.0.0
	 */
	public void delete(final TruckInfo info) {
		try {
			DeleteBuilder<TruckInfo, Integer> deleteBuilder = truckDao
					.deleteBuilder();
			deleteBuilder.where().eq("truckId", info.getTruckId());
			deleteBuilder.delete();
		} catch (SQLException e) {
			LLogger.e("删除用户时发生异常 ： " + e);
		}
	}

	/**
	 * 是否需要初始化省市 isNeedInit(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @return boolean
	 * @exception
	 * @since 1.0.0
	 */
	public boolean isNeedInit() {
		try {
			if (truckDao.isTableExists()) {
				if (truckDao.queryForAll().size() == 0) {
					return true;
				}
			} else {
				TableUtils.createTable(LDaoManager.connectionSource,
						TruckInfo.class);
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 
	 * syncData(这里用一句话描述这个方法的作用) 批量插入用户 (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param infos
	 *            void
	 * @exception
	 * @since 1.0.0
	 */
	public void syncData(final List<TruckInfo> infos) {
		if (infos == null || infos.size() == 0)
			return;
		try {
			truckDao.callBatchTasks(new Callable<Void>() {

				@Override
				public Void call() throws Exception {
					// userDao.deleteBuilder().delete();
					for (TruckInfo info : infos) {
						truckDao.create(info);
					}
					return null;
				}
			});
		} catch (Exception e) {
			LLogger.e("保存数据类型时发生异常 ： " + e);
		}
	}

}
